# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import sqlite3
from RoomPriceChongqing import settings

class RoompricechongqingPipeline(object):
    def __init__(self):
        self.con = sqlite3.connect(settings.db_name)
        self.cur = self.con.cursor()
        self.start_number = 1
        try:
            self.cur.execute("create table chongqingprice(ID integer,name text,price integer)")
        except:
            self.cur.execute("select * from chongqingprice order by id desc limit 0,1")
            try:
                for item in self.cur:
                    self.start_number = item[0]+1
            except:
                # self.start_number = 1
                pass

    def process_item(self, item, spider):
        if item['price']:
            data = (self.start_number,str(item['name']),int(item['price']))
            self.cur.execute("insert into chongqingprice values(?,?,?)",data)
            self.con.commit()
            self.start_number +=1
            return item
        else:
            return item
    def close_spider(self,spider):
        self.con.close()